
function isNumeric(num) {
    return !isNaN(num);
}

function clean() {
    $('#departueDateField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#arrivalDateField').parent().removeClass('has-error has-feedback').find('span').remove();

    $('#descriptionField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#carDropDown').parent().removeClass('has-error has-feedback').find('span').remove();

    $('#startingCountryDropDown').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#startingAddressDropDown').parent().removeClass('has-error has-feedback').find('span').remove();

    $('#destinyCountryDropDown').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#destinyAddressDropDown').parent().removeClass('has-error has-feedback').find('span').remove();
}

function deepClean() {
    clean();
    $('#departueDateField').val("");
    $('#arrivalDateField').val("");

    $('#descriptionField').val("");
}

function validate() {
    clean();
    var validator = {valid: false};

    var departueDateField = $('#departueDateField').val();
    if (!departueDateField) {
        $('#departueDateField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Insert a date!</span>').insertAfter('#departueDateField');
        return validator;
    }
    var departueDate = new Date(Date.parse(departueDateField));
    if (!departueDate) {
        $('#departueDateField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Insert a date!</span>').insertAfter('#departueDateField');
        return validator;
    }

    var arrivalDateField = $('#arrivalDateField').val();
    if (!arrivalDateField) {
        $('#arrivalDateField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Insert a date!</span>').insertAfter('#arrivalDateField');
        return validator;
    }
    var arrivalDate = new Date(Date.parse(arrivalDateField));
    if (!arrivalDate) {
        $('#departueDateField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Insert a date!</span>').insertAfter('#departueDateField');
        return validator;
    }

    if (arrivalDate < departueDate) {
        $('#arrivalDateField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Date must be greater that departue date!</span>').insertAfter('#arrivalDateField');
        return validator;
    }

    var descriptionField = $('#descriptionField').val();
    if (!descriptionField) {
        $('#descriptionField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Description must have 1-700 characters!</span>').insertAfter('#descriptionField');
        return validator;
    }
    if (descriptionField.length > 700) {
        $('#descriptionField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Description must have 1-700 characters!</span>').insertAfter('#descriptionField');
        return validator;
    }
    if ($("#carDropDown").val() === "-1") {
        $('#carDropDown').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Not a valid car!</span>').insertAfter('#carDropDown');
        return validator;
    }


    if ($("#startingCountryDropDown").val() === "-1") {
        $('#startingCountryDropDown').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Not a valid car!</span>').insertAfter('#startingCountryDropDown');
        return validator;
    }
    if ($("#startingAddressDropDown").val() === "-1") {
        $('#startingAddressDropDown').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Not a valid car!</span>').insertAfter('#startingAddressDropDown');
        return validator;
    }

    if ($("#destinyCountryDropDown").val() === "-1") {
        $('#destinyCountryDropDown').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Not a valid car!</span>').insertAfter('#destinyCountryDropDown');
        return validator;
    }
    if ($("#destinyAddressDropDown").val() === "-1") {
        $('#destinyAddressDropDown').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Not a valid car!</span>').insertAfter('#destinyAddressDropDown');
        return validator;
    }

    if ($("#destinyAddressDropDown").val() === $("#startingAddressDropDown").val()) {
        $('#destinyAddressDropDown').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Destiny must be diferent from starting place!</span>').insertAfter('#destinyAddressDropDown');
        return validator;
    }

    validator["departueDateField"] = departueDateField;
    validator["arrivalDateField"] = arrivalDateField;

    validator["descriptionField"] = descriptionField;
    validator["carDropDown"] = $("#carDropDown").val();

    validator["startingCountryDropDown"] = $("#startingCountryDropDown").val();
    validator["startingAddressDropDown"] = $("#startingAddressDropDown").val();

    validator["destinyCountryDropDown"] = $("#destinyCountryDropDown").val();
    validator["destinyAddressDropDown"] = $("#destinyAddressDropDown").val();

    validator["valid"] = true;
    return validator;
}

function sendTrip(validator) {
    sendCreateTrip(validator, function (data) {
        if (data["success"] === true) {
            window.location = '/Trabalho_PD/mytrips.html';
        } else {
            $("#createTripButtonArea").append('<span class="glyphicon glyphicon-remove form-control-feedback"></span><span class="help-block">Error! Retry please!</span>');
        }
    });
}

function sendNewTrip() {
    var validator = validate();
    if (validator["valid"]) {
        sendTrip(validator);
    }
}

function loadCountrys() {
    getAllCountrys(function (arrayCountry) {
        for (var i in arrayCountry) {
            $("#startingCountryDropDown").append('<option value="' + arrayCountry[i].id_country + '">' + arrayCountry[i].name + '</option>');
            $("#destinyCountryDropDown").append('<option value="' + arrayCountry[i].id_country + '">' + arrayCountry[i].name + '</option>');
        }
    });
}

$(document).ready(function () {
    $('#departueDateField').datetimepicker({
        closeOnDateSelect:true,
        step: 5,
        startDate: '+1970/01/02',
        defaultDate: '+1970/01/02',
        minDate: '+1970/01/02',
        defaultTime: '09:00'
    });

    $('#arrivalDateField').datetimepicker({
        closeOnDateSelect:true,
        step: 5,
        startDate: '+1970/01/02',
        defaultDate: '+1970/01/02',
        minDate: '+1970/01/02',
        defaultTime: '09:00'
    });

    loadCountrys();

    $("#startingCountryDropDown").change(function () {
        if ($("#startingCountryDropDown").val() !== "-1") {
            getAllAdressFromCountry($("#startingCountryDropDown").val(), function (arrayAdress) {

                $('#startingAddressDropDown')
                        .find('option')
                        .remove()
                        .end()
                        .append('<option value="-1">Select the starting address</option>');

                for (var i in arrayAdress) {
                    $("#startingAddressDropDown").append('<option value="' + arrayAdress[i].id_address + '">' + arrayAdress[i].address + '</option>');
                }
            });
        }
    });

    $("#destinyCountryDropDown").change(function () {
        if ($("#destinyCountryDropDown").val() !== "-1") {
            getAllAdressFromCountry($("#destinyCountryDropDown").val(), function (arrayAdress) {

                $('#destinyAddressDropDown')
                        .find('option')
                        .remove()
                        .end()
                        .append('<option value="-1">Select destiny address</option>');

                for (var i in arrayAdress) {
                    $("#destinyAddressDropDown").append('<option value="' + arrayAdress[i].id_address + '">' + arrayAdress[i].address + '</option>');
                }
            });
        }
    });
});


